package cn.geminis.manage.service.controller;

import cn.geminis.core.data.query.QueryParameters;
import cn.geminis.manage.core.entity.Function;
import cn.geminis.manage.service.model.FunctionRepository;
import cn.geminis.web.logging.LoggingMethod;
import cn.geminis.web.logging.LoggingParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Allen
 */
@RestController
@RequestMapping("/functions")
public class FunctionController {

    @Autowired
    private FunctionRepository repository;

    @PostMapping
    @PreAuthorize("hasAuthority('manage')")
    public Page<Function> findPage(@RequestBody final QueryParameters queryParameters) {
        return this.repository.findPage(queryParameters);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('manage')")
    @LoggingMethod("保存权限")
    public void save(@RequestBody @LoggingParam(value = "ID", field = "id") Function function) {
        this.repository.save(function);
    }

    @DeleteMapping
    @PreAuthorize("hasAuthority('manage')")
    @LoggingMethod("删除权限")
    public void delete(@RequestBody @LoggingParam("ID") List<String> ids) {
        ids.forEach(id -> this.repository.deleteById(id));
    }

}
